﻿

var HXCCWS = {
    Creater: function (pars) {
        var ws = {};

        ws.hxccws;
        ws.weburl = pars.weburl;
        ws.agentcode = pars.agentcode;
//      console.log(pars);
        ws.Connect = function () {
            var support = "MozWebSocket" in window ? 'MozWebSocket' : ("WebSocket" in window ? 'WebSocket' : null);

            if (support == null) {
                alert("此浏览器不支持WebSocket");
                return;
            }

            $.ajax({
                type: "post",
                url: "http://" + ws.weburl + "/phoneapi/Phone/HuaXiangPhone/GetServerIpByAgentCode",
                contentType: "application/json",
                data: JSON.stringify({"AgentCode":ws.agentcode}),
                success: function (data) {
                    var jsonData = jQuery.parseJSON(data);
                    console.log(jsonData);
                    if (jsonData.Key == "OK") {
                        ws.ccip = jsonData.IP;
                        ws.ccport = jsonData.Port;

                        var wsaddress = "ws://" + ws.ccip + ":" + ws.ccport + "/";
                        ws.hxccws = new window[support](wsaddress);

                        ws.hxccws.onmessage = function (evt) {
                            if (typeof (evt.data) == "string") {
                                var jsonData = eval("(" + evt.data + ")");

                                switch (jsonData.Event) {
                                    case "LoginSecc": {
                                        ws.ConnectSuccess();
                                        break;
                                    }
                                    case "CallResult":
                                        {
                                            var iCallResult = jsonData.Result;
                                            var jsResult = jQuery.parseJSON(iCallResult);
                                            if (jsResult.AgentCode == ws.agentcode) {
                                                ws.CallResult(jsResult.ICallResult);
                                            }
                                        }
                                        break;
                                }
                            }
                        };

                        ws.hxccws.onopen = function () {
                            ws.hxccws.send(JSON.stringify({ "Method": "Login", "OtherParameters": JSON.stringify({ "AgentCode": ws.agentcode }) }));
                        };

                        ws.hxccws.onclose = function () { ws.Close(); }

                        ws.hxccws.onerror = function () { ws.Error(); }
                    } else {
                        alert(jsonData.Message);
                    }
                }
            })
        },

        //拨打电话
        ws.CallPhoneByAgent = function (phoneCode) {
            var regNum = /^\d+$/g;
            if (regNum.test(phoneCode)) {
                if (ws.hxccws) {
                    ws.hxccws.send(JSON.stringify({ "Method": "DialCall", "OtherParameters": JSON.stringify({ "AgentCode": ws.agentcode, "PhoneCode": phoneCode }) }));
                }
            }
        },

        //录音查询
        ws.FindPhoneRecords = function (PageIndex, PageNumber, AgentCode, PhoneCode, StartDate, EndDate, CallType, StartLeng, EndLeng) {
            $.ajax({
                type: "post",
                url: "http://" + ws.weburl + "/phoneapi/Phone/HuaXiangPhone/FindPhoneRecords",
                contentType: "application/json",
                data: JSON.stringify({ "PageIndex": PageIndex, "PageNumber": PageNumber, "AgentCode": AgentCode, "PhoneCode": PhoneCode, "StartDate": StartDate, "EndDate": EndDate, "CallType": CallType, "StartLeng": StartLeng, "EndLeng": EndLeng }),
                success: function (data) {

                    ws.FindPhoneRecordsCallBack(data);
                }
            })
        }

        ws.ConnectSuccess = function () { } //连接呼叫系统成功回调函数
        ws.CallResult = function (infoId) { } //拨打电话失败回调函数 infoId=0:表示坐席未摘机，infoId=-2:表示坐席对应的线路断线
        ws.Close = function () { }//呼叫系统关闭回调函数
        ws.Error = function () { }//连接呼叫系统发生错误
        ws.FindPhoneRecordsCallBack = function (data) { }//录音查询成功返回结果

        return ws;
    }
}
